package com.mypack.crm.service.account.impl;

import com.github.pagehelper.PageHelper;
import com.mypack.crm.dto.account.AccountDTO;
import com.mypack.crm.dto.account.AccountSearchDTO;
import com.mypack.crm.mapper.AccountMapper;
import com.mypack.crm.service.account.AccountService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public AccountDTO queryByUsername(String username) {
        if (StringUtils.isEmpty(username)) {
            return new AccountDTO();
        }
        return accountMapper.queryByUsername(username);
    }

    @Override
    public List<AccountDTO> listAccount(AccountSearchDTO searchDTO, boolean isPage) {

        if (isPage) {
            PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize());
        }


        List<AccountDTO> accountDTOList = accountMapper.listAccount(searchDTO);

        if (CollectionUtils.isEmpty(accountDTOList)) {
            return accountDTOList;
        }

        return accountDTOList;
    }
}
